package net.sea.setup.ui.wizard;

import java.awt.Color;
import java.util.List;
import java.util.Map;

import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import org.apache.log4j.Logger;

import net.sea.commons.constants.AppConstants;
import net.sea.commons.context.AppContext;
import net.sea.setup.config.custom.CustomConfig;

/**
 * 抽象向导页
 * 
 * @author 华成伍
 *
 */
@SuppressWarnings("serial")
public abstract class AbstractWizardPage extends JPanel implements IWizardPage {
	protected Logger logger = Logger.getLogger(getClass());
	/**
	 * 向导页父容器
	 */
	private WizardPanel parent;
	// 获取上下文实例对象
	protected static AppContext contextInst = AppContext.getInstance();

	protected AbstractWizardPage() {
		this.setOpaque(false);
	}

	/**
	 * 向导页页码
	 */
	protected int index;

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	@Override
	public void ready() {
		contextInst.setAttribute(AppConstants.NEEDEDS_BUTTONS_KEY, null);
		process();
	}

	@Override
	public void process() {

	}

	@SuppressWarnings("unchecked")
	@Override
	public void done() {
		Map<String, CustomConfig> customMap = (Map<String, CustomConfig>) contextInst
				.getAttribute(AppConstants.CUSTOM_CONFIG_KEY);
		List<CustomConfig> customConfigs = createCustomConfig();
		if (customConfigs != null) {
			for (CustomConfig customConfig : customConfigs) {
				customMap.put(customConfig.getType(), customConfig);
			}
		}
	}

	protected abstract List<CustomConfig> createCustomConfig();

	public WizardPanel getParent() {
		return parent;
	}

	public void setParent(WizardPanel parent) {
		this.parent = parent;
	}

	/**
	 * 设置边框
	 * 
	 * @param title
	 */
	protected void setBorder(String title) {
		setBorder(new TitledBorder(new LineBorder(Color.GRAY),
				title, TitledBorder.LEADING, TitledBorder.TOP, null, null));
	}
}
